package www.ym.com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import www.ym.com.entity.Company;
import www.ym.com.mapper.CompanyMapper;
import www.ym.com.service.CompanyService;


@Service
@Slf4j
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Page<Company> pageCompany(JSONObject jsonObject) {

        //当前页数
        Long pageNum = Long.valueOf(String.valueOf(jsonObject.get("pageNum")));
        //每页显示数
        Long pageSize = Long.valueOf(String.valueOf(jsonObject.get("pageSize")));
        //企业名称
        String enterpriseName = String.valueOf(jsonObject.get("enterpriseName"));
        //企业状态
//        Integer status = Integer.valueOf(String.valueOf(jsonObject.get("status")));
        Object status = jsonObject.get("status");

        log.info("当前页为" + pageNum);
        log.info("每页显示数" + pageSize);

        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(enterpriseName != "", Company::getEnterpriseName, enterpriseName);
        if (!status .equals("")) {
            wrapper.eq(Company::getStatus,status);
        } else {
            wrapper.in(Company::getStatus, 0, 2);
        }
//        wrapper.orderByDesc(Company::getCreateTime);
        //进行分页查询-可跟条件
        Page<Company> urf = companyMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        System.out.println("数据为：" + urf.getRecords());
        System.out.println("总数为：" + urf.getTotal() + ",总页数为：" + urf.getPages());
        System.out.println("当前页为：" + urf.getCurrent() + ",每页限制：" + urf.getSize());
        return urf;

    }
}
